package bag

import (
	"game/game-db/mysql/dao"
	"sync"
	"tcs-server/tcs-game-server/src/foundation/starting"
	bag "tcs-server/tcs-game-server/src/module/bag/model"
)

var singleton *bagService = &bagService{}
var once sync.Once

func Service() *bagService {
	once.Do(func() {
		var service bagService
		service.idao = dao.NewDefaultBaseDao[*bag.Bag](starting.DataDB, &bag.Bag{})
		singleton = &service
	})
	return singleton
}

type bagService struct {
	idao dao.IDao[*bag.Bag]
}

func (s *bagService) GetEntity(playerId int64) *bag.Bag {
	entity := s.idao.GetByPrimaryKey(playerId)
	if entity == nil {
		entity = &bag.Bag{}
		entity.PlayerId = playerId
		entity.Items = make(map[int32]int64)
		s.idao.Insert(entity)
	}
	return entity
}
